package com.weige.conver.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weige.conver.config.ActivitiConfig;
import com.weige.conver.entity.EnvData;
import com.weige.conver.entity.PosInfo;
import com.weige.conver.entity.ReceiveMessage;
import com.weige.conver.service.DeviceService;
import com.weige.conver.service.EnvDataService;
import com.weige.conver.service.RabbitMQService;
import com.weige.conver.utils.DateUtils;
import com.weige.conver.utils.FileUtils;
import lombok.SneakyThrows;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制器状态监测设备物模型转换-报警类
 */
public class FireAlarmlHandler implements ConverHandler {

    RabbitMQService rabbitMQService = ActivitiConfig.getBean(RabbitMQService.class);
    DeviceService deviceService = ActivitiConfig.getBean(DeviceService.class);
    EnvDataService envDataService = ActivitiConfig.getBean(EnvDataService.class);

//    @SneakyThrows
//    @Override
//    public void handler(ReceiveMessage receiveMessage) throws IOException {
//        String deviceId = receiveMessage.getDevId();
//        String deviceName = receiveMessage.getDevName();
//        String value = receiveMessage.getValue();
//        List point = JSONArray.parseArray(value);
//        //物联网平台数据配置
//        String model = "fire_alarm";
//        String manufactureId = "ShuyanyuanAIGateway";
//        String serviceName = "envDataService";
//        //********
//        Map siteMap = FileUtils.getBaseMap();
//        PosInfo posInfo = new PosInfo();
//        posInfo.setSiteId((String) siteMap.get("siteId"));
//        posInfo.setSiteName((String) siteMap.get("siteName"));
//        posInfo.setObjectId(deviceId);
//        posInfo.setObjectName(deviceName);
//        posInfo.setMonitorDate(DateUtils.getNowTime());
//        posInfo.setObjectType(model);
//        posInfo.setPoints(point);
//        List<PosInfo> posInfoList = new ArrayList<>();
//        posInfoList.add(posInfo);
//        EnvData env = new EnvData();
//        env.setEnvDataList(posInfoList);
//        env.setTopicName("EnvMonitor_envDataService");
//        //判断数据对象是否为空，并上传到RabbitMQ
//        if (env != null) {
//            String data = JSONObject.toJSONString(env);
//            rabbitMQService.sendData(data, deviceId, deviceName, model, manufactureId, serviceName);
//        }
//    }


    @SneakyThrows
    @Override
    public void handler(ReceiveMessage receiveMessage) throws IOException {

        String deviceId = receiveMessage.getDevId();
        String deviceName = receiveMessage.getDevName();
        //物联网平台数据配置
        String model = "fire_alarm";
        String manufactureId = "ShuyanyuanAIGateway";
        String serviceName = "envDataService";
        EnvData env = envDataService.getEnvDataByAction(receiveMessage,model,"fa","火警告警值","火警告警状态监测","");
        //判断数据对象是否为空，并上传到RabbitMQ
        if (env != null){
            String data = JSONObject.toJSONString(env);
            rabbitMQService.sendData(data,deviceId,deviceName,model,manufactureId,serviceName);
        }
    }

}
